package com.net.xpay.core.utils;

import com.google.common.base.Strings;


import java.util.concurrent.atomic.AtomicLong;


/**
 * id生成器
 */
public class GenIdUtil {

    private static final AtomicLong INC = new AtomicLong(0);

    /**
     * 生成id
     * 规则：PREFIX当前时间戳+用户后6位+ 4位递增序列 + 6位随机数
     *
     * @return id
     */
    public static String getId(String prefix, Long uid) {
        StringBuilder builder = new StringBuilder(prefix);
        builder.append(System.currentTimeMillis());
        String id = Strings.padStart("" + uid % 1000000, 6, '0');
        String inc = Strings.padStart("" + Math.abs(INC.getAndIncrement() % 10000), 4, '0');
        String seq = Strings.padStart("" + (long) (Math.random() * 1000000), 6, '0');

        return builder.append(id).append(inc).append(seq).toString();
    }

}
